package com.gxgeek.javabasic.javakeywords.volatiles;

import lombok.extern.slf4j.Slf4j;

/**
 * Created by GX on 2017/8/30.
 */
@Slf4j
public class VolatileExample extends Thread{
    //设置类静态变量,各线程访问这同一共享变量
    private  static  boolean flag = false;
    //无限循环,等待flag变为true时才跳出循环
    public void run() {
        while (!flag){
        };
        log.info("停止了");
    }

    public static void main(String[] args) throws Exception {
        new VolatileExample().start();
        //sleep的目的是等待线程启动完毕,也就是说进入run的无限循环体了
        Thread.sleep(100);
        flag = true;
    }
}
